/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.configGenerator;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.entity.config.CefExtendConfig;
import com.inspur.edp.cef.entity.dependenceTemp.DataValidator;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public abstract class GetExtendConfigsMethodGenerator extends ClassMethodGenerator {
  protected GetExtendConfigsMethodGenerator() {}

  @Override
  protected Block buildMethodBody() {
    java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> statements =
        new java.util.ArrayList<org.eclipse.jdt.core.dom.Statement>();
    addNewListStatement(statements);
    addBuildListStatements(statements);
    addReturnStatements(statements);
    Block block = ast.newBlock();
    block.statements().addAll(statements);
    return block;
  }

  private void addNewListStatement(java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> list) {
    VariableDeclarationFragment declarationFragment = ast.newVariableDeclarationFragment();
    declarationFragment.setName(ast.newSimpleName("list"));
    ClassInstanceCreation instanceCreation = ast.newClassInstanceCreation();
    ParameterizedType creationType =
        ast.newParameterizedType(ast.newSimpleType(ast.newName("java.util.ArrayList")));
    creationType
        .typeArguments()
        .add(ast.newSimpleType(ast.newName("com.inspur.edp.cef.entity.config.CefExtendConfig")));
    instanceCreation.setType(creationType);
    declarationFragment.setInitializer(instanceCreation);
    VariableDeclarationStatement declarationStatement =
        ast.newVariableDeclarationStatement(declarationFragment);
    ParameterizedType creationType1 =
        ast.newParameterizedType(ast.newSimpleType(ast.newName("java.util.ArrayList")));
    creationType1
        .typeArguments()
        .add(ast.newSimpleType(ast.newName("com.inspur.edp.cef.entity.config.CefExtendConfig")));
    declarationStatement.setType(creationType1);
    list.add(declarationStatement);
  }

  private void addBuildListStatements(
      java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> list) {
    if (getHasConfig() == false) {
      return;
    }
    CefExtendConfig config = getConfig();
    DataValidator.checkForNullReference(config, "CefConfig");
    addBuildConfigStatements(list, config);
  }

  //
  //        CefExtendConfig config =new CefExtendConfig();
  //        config.getID()="ID";
  //        config.SourceID="SourceID";
  //        config.DefaultNamespace="DefaultNamespace";
  //        config.MgrConfig=new MgrConfig();
  //        config.MgrConfig.Assembly="Assembly";
  //        config.MgrConfig.Class="Class";
  //        config.MgrConfig.ImpAssembly="ImpAssembly";
  //        config.MgrConfig.ImpClass="ImpClass";
  //        config.MgrConfig.AllInterfaceAssembly="AllInterfaceAssembly";
  //        config.MgrConfig.AllInterfaceClassName="AllInterfaceClassName";
  //        config.RepositoryConfig=new RepositoryConfig();
  //        config.RepositoryConfig.Assembly="Assembly";
  //        config.RepositoryConfig.Class="Class";
  //
  private void addBuildConfigStatements(
      java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> list, CefExtendConfig config) {
    addDeclareCefConfig(list, config);
    addSetIDAttribute(list, config);
    addSetSourceIDAttribute(list, config);
    GetConfigsMethodGenerator.addSetMgrConfigAttributeStatement(ast, list, config.getMgrConfig());
    GetConfigsMethodGenerator.addSetRepositoryConfigAttributeStatement(
        ast, list, config.getRepositoryConfig());
  }

  private void addDeclareCefConfig(
      java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> list, CefExtendConfig config) {
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    SimpleType classInstanceCreationType = ast.newSimpleType(ast.newSimpleName("CefExtendConfig"));
    classInstanceCreation.setType(classInstanceCreationType);
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName("config"));
    variableDeclarationFragment.setInitializer(classInstanceCreation);
    list.add(ast.newVariableDeclarationStatement(variableDeclarationFragment));
  }

  // region 简单属性
  private void addSetIDAttribute(
      java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> list, CefExtendConfig config) {
    MethodInvocation setIdInvocation = ast.newMethodInvocation();
    setIdInvocation.setExpression(ast.newSimpleName("config"));
    setIdInvocation.setName(ast.newSimpleName("setID"));
    StringLiteral idLiteral = ast.newStringLiteral();
    idLiteral.setLiteralValue(config.getID());
    setIdInvocation.arguments().add(idLiteral);
    list.add(ast.newExpressionStatement(setIdInvocation));
  }

  private void addSetSourceIDAttribute(
      java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> list, CefExtendConfig config) {
    MethodInvocation setInvocation = ast.newMethodInvocation();
    setInvocation.setExpression(ast.newSimpleName("config"));
    setInvocation.setName(ast.newSimpleName("setSourceConfigID"));
    StringLiteral valueLiteral = ast.newStringLiteral();
    valueLiteral.setLiteralValue(config.getSourceConfigID());
    setInvocation.arguments().add(valueLiteral);
    list.add(ast.newExpressionStatement(setInvocation));
  }

  // endregion

  private void addReturnStatements(java.util.ArrayList<org.eclipse.jdt.core.dom.Statement> list) {
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName("list"));
    list.add(returnStatement);
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>(
        java.util.Arrays.asList(new ModifierKeyword[] {ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected String getMethodName() {
    return "getExtendConfigs";
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return new java.util.ArrayList<ParameterInfo>();
  }

  @Override
  protected TypeInfo getReturnType() {
    TypeInfo info = new TypeInfo(java.util.ArrayList.class);
    info.setIsParameterizedType(true);
    ArrayList<TypeInfo> genericTyeps = new ArrayList<>(2);
    genericTyeps.add(new TypeInfo(CefExtendConfig.class));
    info.setArguments(genericTyeps);
    return info;
  }

  protected abstract boolean getHasConfig();

  protected abstract CefExtendConfig getConfig();
//
//  @Override
//  protected final boolean getIsOverride() {
//    return true;
//  }
}
